package mime.bean;

import javassist.*;
import org.junit.Test;

import java.io.IOException;
import java.util.Random;

/**
 * 广州奥凯信息咨询有限公司
 * <p>
 * 本源代码由《项目一组》及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (广州奥凯信息咨询有限公司) 2019-2022. all rights reserved.
 */

public class Demo {

    private final static String packagePath = "javassist";
    @Test
    public void defineNew() throws IOException, CannotCompileException, NotFoundException {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass = pool.makeClass(packagePath+".CtClass" + new Random().nextInt());
        ctClass.writeFile();
    }

    @Test
    public void changeSupper() throws NotFoundException, CannotCompileException, IOException {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.get("mime.Undergraduate");
        CtClass personClass = classPool.get("mime.Person");
        ctClass.setSuperclass(personClass);
        ctClass.writeFile(packagePath);
        System.out.println(ctClass.toClass());
    }

    @Test
    public void bean() throws NotFoundException, CannotCompileException, IOException {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.makeInterface(packagePath + ".BeanInterface2");
        CtClass serializable = classPool.get("java.io.Serializable");
        ctClass.setSuperclass(serializable);

        CtMethod method = CtNewMethod.make("String echo(String echo){return echo;}", ctClass);
        ctClass.addMethod(method);
        CtMethod method2 = CtNewMethod.make("String echo2(String echo){return echo;}", ctClass);
        ctClass.addMethod(method2);
        CtMethod hi = CtNewMethod.make("String hi();",ctClass);
        ctClass.addMethod(hi);

        ctClass.writeFile();

    }
}
